package com.kgd.news.utils;

import com.alibaba.fastjson.JSONObject;

/**
 * api接口返回数据
 * 
 * @author huangsanm 返回的格式： {msg:'请求成功|失败原因',data:data,code:代码}
 */
public class Api {

	public static final int S_200 = 200;
	// 异常错误
	public static final int E_201 = 201;
	// 参数错误
	public static final int A_202 = 202;
	// 逻辑错误
	public static final int L_203 = 203;
	// 数据为空
	public static final int D_204 = 204;
	// 重复数据
	public static final int R_205 = 205;
	// 逻辑处理数据
	public static final int L_206 = 206;
	// 数据库错误
	public static final int D_207 = 207;
	// 逻辑处理数据
	public static final int L_404 = 404;
	// 该orderid的贺卡已经生成
	public static final int L_210 = 210;

	private String msg = "请求成功";
	private Object data;
	// 默认请求成功
	private int code = 200;

	public Api(){
		this(new JSONObject());
	}

	public Api(Object data) {
		this(200, data, "请求成功");
	}

	public Api(String msg) {
		this(200, new JSONObject(), msg);
	}

	public Api(int code, String msg) {
		this(code, new JSONObject(), msg);
	}

	public Api(int code, Object data) {
		this(code, data, "请求成功");
	}

	public Api(Object data, String msg) {
		this(200, data, msg);
	}

	public Api(int code, Object data, String msg) {
		this.code = code;
		this.data = data;
		this.msg = msg;
	}

	public JSONObject get() {
		JSONObject response = new JSONObject();
		response.put("code", getCode());
		response.put("data", getData());
		response.put("msg", getMsg());
		return response;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

}
